package com.eastmoney.emis.edda.projection;

import com.eastmoney.emis.edda.domain.event.EddaEvent;
import com.eastmoney.emis.edda.domain.event.EddaFailEvent;
import com.eastmoney.emis.edda.domain.repository.HsbcEddaFlowRepository;
import com.eastmoney.emis.edda.infrastructure.edda.po.EmhkEddaFlowPo;
import com.eastmoney.emis.edda.task.TaskManagement;
import com.eastmoney.emis.edda.task.edda.EddaFailSmsTask;
import com.eastmoney.emis.logger.LoggerFactory;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

/**
 * @description: Description
 * @projectName: EDDA-HSBC
 * @package: com.eastmoney.emis.edda.domain
 * @author: 汪程程
 * @date: 2022/9/30 2:05 PM
 * @version: 1.0
 */
@Component
public class EddaEventListener {
    private static final Logger log = LoggerFactory.getLogger(EddaEventListener.class);

    @Autowired
    private HsbcEddaFlowRepository repository;

    @Autowired
    private TaskManagement taskManagement;

    @EventListener
    public void handlerEddaEvent(EddaEvent event) {
        log.info("process_user = {}, client_id = {}, process event = {}[{}], status = {}",
                event.getProcessUserName(), event.getClientId(),
                event.getClass().getSimpleName(), event.getResult(), event.getStatus());
        EmhkEddaFlowPo flowPo = new EmhkEddaFlowPo();
        flowPo.HKCUACCID = event.getClientId();
        flowPo.OPERATION = event.getOperation();
        flowPo.RESULT = event.getResult();
        flowPo.REFEID = event.getRefEid();
        flowPo.NOTE = event.getNote();
        flowPo.PROCESSUSERID = event.getProcessUid();
        flowPo.PROCESSUSERNAME = event.getProcessUserName();
        flowPo.STATUS = event.getStatus();
        repository.addFlow(flowPo);
    }

    @EventListener
    public void handlerEddaFailEvent(EddaFailEvent event) {
        taskManagement.addTask(new EddaFailSmsTask(event.getEid(), event.getClientId(), event.getParams()));
    }

}
